'use strict';

const conn = require('../../../libs/Conn');

module.exports = async function (params) {
    /*排序*/
    let orderBySql = ``;
    if(params.sort&&params.dir){
        switch (params.sort) {
            case 'department_name':
                orderBySql = `ORDER BY department_name ${params.dir}`;
                break;
            case 'departmental_functions':
                orderBySql = `ORDER BY departmental_functions ${params.dir}`;
                break;
        }
    }
    /*分页*/
    let pageSql = `LIMIT ${params.pageNum>1?((params.pageNum-1)*10):0},10;SELECT FOUND_ROWS() AS count`;

    let querySql = `SELECT SQL_CALC_FOUND_ROWS department_id,department_name,departmental_functions FROM department_file WHERE delete_sign = 1 ${orderBySql} ${pageSql}`;
    let rec = await conn(querySql);
    let pageNum = Math.ceil(rec[1][0].count/10);
    if(rec[0].length>0){
        rec[0][0].dataTotal = rec[1][0].count;
        rec[0][0].pageTotal = pageNum;
        rec[0][0].pageNum = params.pageNum;
    }
    return {success: true, data: rec[0]};

};